#include "admin.h"
#include "modify.h"
#include "system.h"
#include "ui_admin.h"
#include <QDebug>
#include <QMenu>
#include <QSqlQuery>

Admin::Admin(QWidget* parent) :
    QWidget(parent),
    ui(new Ui::Admin)
{
    ui->setupUi(this);

    ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);

    table_widget_menu = new QMenu(ui->tableWidget);
    QAction* action = new QAction("修改", this);
    connect(action, SIGNAL(triggered()), this, SLOT(slotActionModify()));
    table_widget_menu->addAction(action);
    action = new QAction("删除", this);
    connect(action, SIGNAL(triggered()), this, SLOT(slotActionRemove()));
    table_widget_menu->addAction(action);

    qDebug() << QSqlDatabase::drivers();
    m_db = QSqlDatabase::addDatabase("QMYSQL");
    m_db.setHostName("localhost");//主机名
    m_db.setPort(3306);
    m_db.setDatabaseName("user");
    m_db.setUserName("root");
    m_db.setPassword("123456");
    m_db.open();
    if(m_db.open())
    {
        qDebug() << "连接成功";
        showStuInfo();
    }
    else
    {
        qDebug() << "连接失败！！";
    }
}

Admin::~Admin()
{
    delete ui;
}

void Admin::showStuInfo()
{
    QSqlQuery query;
    query.exec("SELECT * FROM users");
    while(query.next())
    {
        QString username = query.value(0).toString();
        QString password = query.value(1).toString();
        qDebug() << QString("UserName %1 Password %2 ").arg(username).arg(password);

        QTableWidgetItem* usernameItem = new QTableWidgetItem(username);
        QTableWidgetItem* passwordItem = new QTableWidgetItem(password);

        int row = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row);
        ui->tableWidget->setItem(row, 0, usernameItem);
        ui->tableWidget->setItem(row, 1, passwordItem);
    }
}
//查找按钮
void Admin::on_pushButtonSeek_clicked()
{

    QString info = ui->lineEditSeekName->text();
    QSqlQuery query;
    query.prepare("SELECT * FROM users WHERE UserName  LIKE ? ");
    query.addBindValue("%" + info + "%");
    if(query.exec())
    {
        qDebug() << "更新成功！";
        ui->tableWidget_2->clearContents();
        ui->tableWidget_2->setRowCount(0);
        while(query.next())
        {
            QString username = query.value(0).toString();
            QString passsword = query.value(1).toString();

            qDebug() << QString("UserName %1 Password %2").arg(username).arg(passsword);

            QTableWidgetItem* usernameItem = new QTableWidgetItem(username);
            QTableWidgetItem* passwordItem = new QTableWidgetItem(passsword);

            int row = ui->tableWidget_2->rowCount();
            ui->tableWidget_2->insertRow(row);
            ui->tableWidget_2->setItem(row, 0, usernameItem);
            ui->tableWidget_2->setItem(row, 1, passwordItem);

        }
    }
    else
    {
        qDebug() << "更新失败！";
    }

}

void Admin::on_pushButton_clicked()
{
    System* m = new System();
    m->show();
    this->close();
}
void Admin::slotActionModify()
{
    on_pushButtonModify_clicked();
}
//修改按钮
void Admin::on_pushButtonModify_clicked()
{

    Modify* m = new Modify();
    m->show();
    this->close();
}
//void Admin::sendSlot()
//{
//    QList<QTableWidgetItem*> selectedItem = ui->tableWidget->selectedItems();
//    if(selectedItem.isEmpty())
//    {
//        return;
//    }
//    int row = ui->tableWidget->currentRow();
//    qDebug() << "on modifybtn clicked" << row;

//    QString username = ui ->tableWidget ->item(row, 0)->text();
//    QString password = ui ->tableWidget ->item(row, 1)->text();
//    emit sendData(username, password);
//}

void Admin::slotActionRemove()
{
    on_pushButtonDel_clicked();
}
//删除按钮
void Admin::on_pushButtonDel_clicked()
{

    qDebug() << "删除按钮被点击了！";
    QList<QTableWidgetItem*> selectedItem = ui->tableWidget->selectedItems();
    if(selectedItem.isEmpty())
    {
        qDebug() << "未选中内容！！！";
        return;
    }

    int row = ui->tableWidget->currentRow();
    qDebug() << "on modifybtn clicked" << row;
    QString username = ui ->tableWidget ->item(row, 0)->text();
    QString password = ui ->tableWidget ->item(row, 1)->text();

    QSqlQuery query;

    query.prepare("DELETE FROM users WHERE UserName=? AND Password=?");
    query.addBindValue(username);
    query.addBindValue(password);
    if(query.exec())
    {
        qDebug() << "更新成功！";
        ui->tableWidget->removeRow(row);
    }
    else
    {
        qDebug() << "更新失败！";
    }


}

void Admin::on_tableWidget_customContextMenuRequested(const QPoint& pos)
{

    table_widget_menu->exec(QCursor::pos());
}
